package com.坦克大战.Tank_6;

import java.awt.*;
import java.io.*;
import java.util.Vector;

@SuppressWarnings("all")
public class Recorder {
    private static int NUMBER_OF_RECORDER = 0;  /// 击毁数量
    static String FilePath = "java-code/src/com/坦克大战/TankGameResults.txt";  ///文件路径
    static Vector<EnemyTanks> enemyTanks = new Vector<>();  ///在保存时用以遍历的敌方坦克集合；
    static Vector<Node> nodes = new Vector<>();             /// 用于保存恢复的坦克节点的集合

    static BufferedReader br = null;  ///输入流
    static BufferedWriter bw = null;  ///输出流

    public static int getNumberOfRecorder() {
        return NUMBER_OF_RECORDER;
    }

    public static void setNumberOfRecorder(int numberOfRecorder) {
        NUMBER_OF_RECORDER = numberOfRecorder;
    }

    public static void changeNumberOfRecorder() {  /// 击毁适量
        NUMBER_OF_RECORDER++;
    }

    public static void setEnemyTanks(Vector<EnemyTanks> e){
        enemyTanks = e;
    }

    public static void RecordTankGameScores() {  /// 记录方法
        try {
            bw = new BufferedWriter(new FileWriter(FilePath));  ///创建文件
            bw.write(NUMBER_OF_RECORDER+"");  ///写入坦克击毁数量
            bw.newLine();                          ///换行
            System.out.println("游戏成绩录入成功");

            for (int i = 0; i < enemyTanks.size(); i++) {   /// 循环输入坦克信息
                EnemyTanks e = enemyTanks.get(i);
                String str = e.getX()+" "+e.getY()+" " + e.getDirection(); ///将该坦克的信息拼接起来，中间空格便于分割
                bw.write(str);  ///写入
                bw.newLine();   ///换行
            }
            System.out.println("坦克坐标录入成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bw.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }


    public static Vector<Node> getNodes(){
        String line = "";
        try {
            br = new BufferedReader(new FileReader(FilePath));   ///如果这里抛出异常，Mypanel里设置了捕获异常，如果找不到文件，则默认开启新游戏
            NUMBER_OF_RECORDER = Integer.parseInt(br.readLine());  ///把第一行的数量先排除；
            while((line = br.readLine())  != null){
                String[] str = line.split(" ");   /// 用空格分割字符串；
                int x = Integer.parseInt(str[0]);
                int y = Integer.parseInt(str[1]);
                int direction = Integer.parseInt(str[2]);
                Node node = new Node(x, y, direction);   ///将每个坦克的信息封装到Node类中，方便遍历；
                nodes.add(node);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return nodes;
    }




}
